package com.hyx.bookManager.controller;

import com.hyx.bookManager.domain.common.CommonResult;
import com.hyx.bookManager.service.MonitorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Map;

/**
 * @ClassName MonitorController
 * @Description TODO
 * @Author 黄乙轩
 * @Date 2021/6/28 22:03
 * @Version 1.0
 **/

@RestController
@CrossOrigin
@RequestMapping("monitor")
public class MonitorController {

    @Autowired
    private MonitorService monitorService;

    @ResponseBody
    @RequestMapping("/getMonitor")
    public CommonResult getMonitor(@RequestBody Map<String,String> map){
        String start = map.get("start");
        String end = map.get("end");
        String page = map.get("page");
        String pageSize = map.get("pageSize");
        LocalDate startTime,endTime;
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        if(start==null||start.equals("Invalid date")){
            startTime=null;
        }else{
            startTime=LocalDate.parse(start,formatter);
        }
        if(end==null||end.equals("Invalid date")){
            endTime=null;
        }else{
            endTime=LocalDate.parse(end,formatter);
        }
        return monitorService.getMonitor(startTime,endTime,Long.valueOf(page),Long.valueOf(pageSize));
    }

    @ResponseBody
    @RequestMapping("/getTotalPage")
    public CommonResult getTotalPage(@RequestBody Map<String,String> map){
        String start = map.get("start");
        String end = map.get("end");
        String page = map.get("page");
        String pageSize = map.get("pageSize");
        LocalDate startTime,endTime;
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        if(start==null||start.equals("Invalid date")){
            startTime=null;
        }else{
            startTime=LocalDate.parse(start,formatter);
        }
        if(end==null||end.equals("Invalid date")){
            endTime=null;
        }else{
            endTime=LocalDate.parse(end,formatter);
        }
        return monitorService.getTotalPage(startTime,endTime,Long.valueOf(page),Long.valueOf(pageSize));
    }
}
